﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using IBuy.DataAccessLayer;
using System.Data;
using System.Windows.Forms;

namespace IBuy.Entity
{
    class Goods
    {
        #region 私有成员
        private string name;
        private string type;
        private float price;
        private int stock;
        private int delivery;
        private IDatabase database;
        #endregion

        #region 公有属性
        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        /// <summary>
        /// 种类
        /// </summary>
        public string Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }
        /// <summary>
        /// 价格
        /// </summary>
        public float Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
        /// <summary>
        /// 库存
        /// </summary>
        public int Stock
        {
            get
            {
                return stock;
            }
            set
            {
                stock = value;
            }
        }
        /// <summary>
        /// 发货时间
        /// </summary>
        public int Delivery
        {
            get
            {
                return delivery;
            }
            set
            {
                delivery = value;
            }
        }
        #endregion

        #region 公有方法
        public Goods()
        {
            DatabaseFactory databaseFactory = new OledbDatabaseFactory();
            database = databaseFactory.GetDatabase();
        }

        public bool InsertGoods(Hashtable values)
        {
            if (database.Insert("GOODS", values))
            {
                MessageBox.Show("插入物品成功！");
                return true;
            }
            else
            {
                MessageBox.Show("插入物品失败！");
                return false;
            }
        }

        public bool UpdateGoods(Hashtable columns, string where)
        {
            if (database.Update("GOODS",columns,where))
            {
                MessageBox.Show("更新物品成功！");
                return true;
            }
            else
            {
                MessageBox.Show("更新物品失败！");
                return false;
            }
        }

        public bool DeleteGoods(string name)
        {
            string where = " where NAME='" + name + "'";
            if (database.Delete("GOODS",where))
            {
                MessageBox.Show("删除物品成功！");
                return true;
            }
            else
            {
                MessageBox.Show("删除物品失败！");
                return false;
            }
        }

        public DataSet GetGoodsByName(string name)
        {
            string where = "where NAME='" + name + "'";
            return GetDataSet(where);
        }

        public DataSet GetGoodsByType(string type)
        {
            string where = "where TYPENAME='" + type + "'";
            return GetDataSet(where);
        }

        public DataSet GetNameAndIdOFAllGoods()
        {
            string SqlString = "select GOODSID,NAME from GOODS";
            return database.GetDataSet(SqlString);
        }
        public DataSet GetAllGoods()
        {
            return GetDataSet("");
        }
        private DataSet GetDataSet(string where)
        {
            string SqlString = "select COMPANYNAME,TYPENAME,NAME,PRICE,STOCK,DELIVERY from GOODSVIEW " + where;
            return database.GetDataSet(SqlString);
        }

        public Hashtable GetAllGoods(string colName)
        {
            string sql = "select GOODSVIEW." + colName + ", Count(GOODSVIEW." + colName + ") as Counts from GoodsView group by GOODSVIEW." + colName;
            DataSet dataset = database.GetDataSet(sql);
            DataTable table = dataset.Tables[0];
            Hashtable Goods = new Hashtable();
            foreach (DataRow row in table.Rows)
            {
                Goods.Add(row[0].ToString(), row[1].ToString());
            }
            return Goods;
        }
        #endregion
    }
}
